/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.vehiclebooking;

import au.edu.swin.swinvehicle.bookingtype.BookingTypeEntity;
import au.edu.swin.swinvehicle.corporateclient.CorporateClientEntity;
import au.edu.swin.swinvehicle.driver.DriverEntity;
import au.edu.swin.swinvehicle.eventtype.EventTypeEntity;
import au.edu.swin.swinvehicle.manager.ManagerEntity;
import au.edu.swin.swinvehicle.vehicle.VehicleEntity;
import au.edu.swin.swinvehicle.vehicletype.VehicleTypeEntity;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Administrator
 */
@Stateless
public class VehicleBookingManagerBean implements VehicleBookingManagerRemote {

    @PersistenceContext
    private EntityManager em;

    public void addVehicleBooking(VehicleBookingDetailedTO to) throws VehicleBookingManagerException {
  
         try {
           VehicleBookingEntity vb = new VehicleBookingEntity();

            vb.setData(to);
            
            vb.setBookingType(em.find(BookingTypeEntity.class, to.getBookingTypeId()));
            vb.setEventType(em.find(EventTypeEntity.class, to.getEventTypeId()));
            vb.setVehicleType(em.find(VehicleTypeEntity.class, to.getVehicleTypeId()));            
            vb.setDriver(em.find(DriverEntity.class, to.getDriverId()));
            vb.setVehicle(em.find(VehicleEntity.class, to.getVehicleId()));
            vb.setManager(em.find(ManagerEntity.class, to.getManagerId()));
            vb.setCorporateClient(em.find(CorporateClientEntity.class, to.getCorporateClientId()));

            em.persist(vb);

            
        } catch (Exception e) {
            e.printStackTrace();
            throw new VehicleBookingManagerException("Cannot add Vehicle Booking", e);
        }
         
    }

    public void deleteVehicleBooking(Long id) throws VehicleBookingManagerException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void editVehicleBooking(VehicleBookingDetailedTO to) throws VehicleBookingManagerException {

         try {
            VehicleBookingEntity vb = em.find(VehicleBookingEntity.class, to.getId());
            
            vb.setData(to);
            vb.setBookingType(em.find(BookingTypeEntity.class, to.getBookingTypeId()));
            vb.setEventType(em.find(EventTypeEntity.class, to.getEventTypeId()));
            vb.setVehicleType(em.find(VehicleTypeEntity.class, to.getVehicleTypeId()));
            vb.setDriver(em.find(DriverEntity.class, to.getDriverId()));
            vb.setVehicle(em.find(VehicleEntity.class, to.getVehicleId()));
            vb.setManager(em.find(ManagerEntity.class, to.getManagerId()));
            vb.setCorporateClient(em.find(CorporateClientEntity.class, to.getCorporateClientId()));

            em.persist(vb);


        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot edit Vehicle Booking", e);
        }
         
    }

    public List<VehicleBookingDetailedTO> getAllDetailedVehicleBookings() throws VehicleBookingManagerException {
        List<VehicleBookingDetailedTO> results = null;

        try {
            results = new ArrayList<VehicleBookingDetailedTO>();
            Query allVehicleBookingsQuery = em.createNamedQuery("findAllVehicleBookings");
            List dbResults = allVehicleBookingsQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                VehicleBookingEntity vb = (VehicleBookingEntity) dbResults.get(i);
                results.add(vb.getDetailedData());
            }
        } catch (Exception e) {

            throw new VehicleBookingManagerException("Cannot get all VehicleBookingDetailedTOs", e);

        }
        return results;
    }

    public List<VehicleBookingTO> getAllVehicleBookings() throws VehicleBookingManagerException {
     
        List<VehicleBookingTO> results = null;

        try {
            results = new ArrayList<VehicleBookingTO>();
            Query allVehicleBookingsQuery = em.createNamedQuery("findAllVehicleBookings");
            List dbResults = allVehicleBookingsQuery.getResultList();

            for (int i = 0; i < dbResults.size(); i++) {
                VehicleBookingEntity vb = (VehicleBookingEntity) dbResults.get(i);
                results.add(vb.getData());
            }
        } catch (Exception e) {

            throw new VehicleBookingManagerException("Cannot get all Vehicle Bookings", e);

        }
        return results;
    }

    public VehicleBookingDetailedTO getVehicleBooking(Long id) throws VehicleBookingManagerException {

        VehicleBookingEntity vb = null;
        VehicleBookingDetailedTO to = null;
        try {
            vb = em.find(VehicleBookingEntity.class, id);
            to = vb.getDetailedData();
        } catch (Exception e) {
            throw new VehicleBookingManagerException("Cannot get Vehicle Booking by id " + id.longValue(), e);
        }
        return to;
    }


}
